﻿using ZeldoEngineObjects.Types;

namespace ZeldoEngineServices.Maps
{
    public static class AutotileService
    {
        public static ZeldoRect[] CalculerZones(uint code, int usize)
        {
            var rectangle = new ZeldoRect[4];

            rectangle[0] = new ZeldoRect { x = 0, y = 0 };
            rectangle[1] = new ZeldoRect { x = usize, y = 0 };
            rectangle[2] = new ZeldoRect { x = 0, y = usize };
            rectangle[3] = new ZeldoRect { x = usize, y = usize };

            switch (code & 0x7)
            {
                case 0x1:
                    {
                        rectangle[0] = new ZeldoRect { x = usize * 4, y = usize * 2 };
                        break;
                    }
                case 0x2:
                    {
                        rectangle[0] = new ZeldoRect { x = 0, y = usize * 6 };
                        break;
                    }
                case 0x3:
                    {
                        rectangle[0] = new ZeldoRect { x = usize * 4, y = 0 };
                        break;
                    }
                case 0x4:
                    {
                        rectangle[0] = new ZeldoRect { x = usize * 2, y = usize * 4 };
                        break;
                    }
            }

            switch ((code >> 3) & 0x7)
            {
                case 0x1:
                    {
                        rectangle[1] = new ZeldoRect { x = usize, y = usize * 2 };
                        break;
                    }
                case 0x2:
                    {
                        rectangle[1] = new ZeldoRect { x = usize * 5, y = usize * 6 };
                        break;
                    }
                case 0x3:
                    {
                        rectangle[1] = new ZeldoRect { x = usize * 5, y = 0 };
                        break;
                    }
                case 0x4:
                    {
                        rectangle[1] = new ZeldoRect { x = usize * 3, y = usize * 4 };
                        break;
                    }
            }

            switch ((code >> 6) & 0x7)
            {
                case 0x1:
                    {
                        rectangle[2] = new ZeldoRect { x = usize * 4, y = usize * 7 };
                        break;
                    }
                case 0x2:
                    {
                        rectangle[2] = new ZeldoRect { x = 0, y = usize * 3 };
                        break;
                    }
                case 0x3:
                    {
                        rectangle[2] = new ZeldoRect { x = usize * 4, y = usize };
                        break;
                    }
                case 0x4:
                    {
                        rectangle[2] = new ZeldoRect { x = usize * 2, y = usize * 5 };
                        break;
                    }
            }

            switch ((code >> 9) & 0x7)
            {
                case 0x1:
                    {
                        rectangle[3] = new ZeldoRect { x = usize, y = usize * 7 };
                        break;
                    }
                case 0x2:
                    {
                        rectangle[3] = new ZeldoRect { x = usize * 5, y = usize * 3 };
                        break;
                    }
                case 0x3:
                    {
                        rectangle[3] = new ZeldoRect { x = usize * 5, y = usize };
                        break;
                    }
                case 0x4:
                    {
                        rectangle[3] = new ZeldoRect { x = usize * 3, y = usize * 5 };
                        break;
                    }
            }

            // Cas speciaux :
            if (((code & 0x7) == 0x1) && (((code >> 3) & 0x7) == 0x1))
            {
                rectangle[0] = new ZeldoRect { x = usize * 2, y = usize * 2 };
                rectangle[1] = new ZeldoRect { x = usize * 3, y = usize * 2 };
            }

            // Cas speciaux :
            if ((((code >> 6) & 0x7) == 0x1) && (((code >> 9) & 0x7) == 0x1))
            {
                rectangle[2] = new ZeldoRect { x = usize * 2, y = usize * 7 };
                rectangle[3] = new ZeldoRect { x = usize * 3, y = usize * 7 };
            }

            // Cas speciaux :
            if (((code & 0x7) == 0x2) && (((code >> 6) & 0x7) == 0x2))
            {
                rectangle[0] = new ZeldoRect { x = 0, y = usize * 4 };
                rectangle[2] = new ZeldoRect { x = 0, y = usize * 5 };
            }

            // Cas speciaux :
            if ((((code >> 3) & 0x7) == 0x2) && (((code >> 9) & 0x7) == 0x2))
            {
                rectangle[1] = new ZeldoRect { x = usize * 5, y = usize * 4 };
                rectangle[3] = new ZeldoRect { x = usize * 5, y = usize * 5 };
            }

            rectangle[0].width = usize;
            rectangle[0].height = usize;

            rectangle[1].width = usize;
            rectangle[1].height = usize;

            rectangle[2].width = usize;
            rectangle[2].height = usize;
            
            rectangle[3].width = usize;
            rectangle[3].height = usize;

            return rectangle;
        }
    }
}
